package online.longzipeng.cinemasys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.entity.Vip;
import online.longzipeng.cinemasys.service.VipService;
import online.longzipeng.cinemasys.vo.vip.VipQueryVO;
import online.longzipeng.cinemasys.vo.vip.VipSaveVO;
import online.longzipeng.cinemasys.vo.vip.VipUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lzp
 * @date 2021/4/26 21:15
 */
@RestController
@RequestMapping("/vip")
@Api(value = "VIP相关接口",description = "VIP相关接口",tags = "VIP相关接口")
public class VipController {
    
    @Autowired
    VipService vipService;

    @RequestMapping(value = "/get-list-page",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation("查询所有分类带分页")
    public Result getListPage(@RequestBody VipQueryVO queryVO){
        IPage<Vip> typeIPage = vipService.getListPage(queryVO);
        return Result.success(typeIPage);
    }

    @GetMapping("/get-list-json")
    @NeedPermissions("vip:select")
    @ApiOperation("获取vip的json集合")
    public Result getListJson(){
        return Result.success(vipService.list());
    }

    @GetMapping("/get-by-id")
    @NeedPermissions("vip:select")
    @ApiOperation("通过id查询vip")
    public Result getById(@ApiParam(value = "id",required = true)Long id){
        return Result.success(vipService.getById(id));
    }

    @PostMapping("/save")
    @NeedPermissions("vip:save")
    @ApiOperation("新增vip")
    public Result save(@RequestBody VipSaveVO vipSaveVO){
        Vip vip = new Vip();
        BeanUtils.copyProperties(vipSaveVO,vip);
        vipService.add(vip);
        return Result.success(null,"vip信息添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("vip:delete")
    @ApiOperation("删除vip")
    public Result deleteOne(@ApiParam(value = "vip的id",required = true)Long id){
        try {
            vipService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"vip信息删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("vip:delete")
    @ApiOperation("删除多个vip")
    public Result deleteOne(@ApiParam(value = "vip的id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            vipService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"vip删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("vip:update")
    @ApiOperation("修改vip")
    public Result update(@RequestBody VipUpdateVO vipUpdateVO) {
        Vip vip = new Vip();
        BeanUtils.copyProperties(vipUpdateVO,vip);
        vipService.updateVip(vip);
        return Result.success(null,"vip修改成功！！");
    }
}
